package com.sh.personnel.service.impl;

import com.sh.personnel.domain.ITbMenu;
import com.sh.personnel.mapper.IMenuMapper;
import com.sh.personnel.mapper.IRoleMapper;
import com.sh.personnel.service.IMenuService;
import com.sh.personnel.util.TimeUtil;
import com.sh.personnel.util.TreeUtils;
import com.sh.personnel.vo.dto.MenuDto;
import com.sh.personnel.vo.dto.MenuTreeDto;
import com.sh.personnel.vo.dto.MetaDto;
import com.sh.personnel.vo.dto.SelectTreeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 林思浩
 * @date 2020/03/13 14:14
 */
@Service
public class IMenuServiceImpl implements IMenuService {

    @Autowired
    IRoleMapper roleMapper;
    @Autowired
    IMenuMapper menuMapper;

    @Override
    public List<MenuTreeDto> generateMenuTree(String userId) {
        List<Integer> roleIdList = roleMapper.findRoleMenuIdByUserId(userId);
        List<MenuTreeDto> resultList = null;
        if(!ObjectUtils.isEmpty(roleIdList)) {
            List<MenuTreeDto> menusList = menuMapper.getMenuAllInRoleId(roleIdList);
            resultList = TreeUtils.generateMenuTree(menusList);
        }
        return resultList;
    }

    @Override
    public List<SelectTreeDto> generateSelectMenuTree(String type) {
        List<SelectTreeDto> list = menuMapper.findMenuAllIDAndName(type);
        List<SelectTreeDto> tree = TreeUtils.generateSelectTree(list);
        return tree;
    }

    @Override
    public List<MenuDto> getMenuAllList(String key) {
        List<MenuDto> list = menuMapper.findMenuAllList(key);
        List<MenuDto> menusList = TreeUtils.generateMenuList(list);
        return menusList;
    }

    @Override
    public ITbMenu getMenuById(String id) {
        ITbMenu menu = menuMapper.findMenuById(id);
        return menu;
    }

    @Override
    public boolean addMenu(ITbMenu menu) {
        menu.setCreatetime(TimeUtil.currentMillis());
        int result = menuMapper.insertMenu(menu);
        return result>0 ? true : false;
    }

    @Override
    public boolean updateMenuById(ITbMenu menu) {
        int result = menuMapper.updateMenuById(menu);
        return result>0 ? true : false;
    }

    @Override
    public boolean deleteMenuById(String id) {
        int result = menuMapper.deleteMenuById(id);
        return result>0 ? true : false;
    }
}
